<?php

// +-----------------------------------------------------
// | Name: 星空框架 权限节点模型
// +-----------------------------------------------------
// | Version: V5.0
// +-----------------------------------------------------
// | Author: xkyinzhe <zhonghuaxinxing@sina.cn>
// +-----------------------------------------------------
// | Time: 2021-04-21 10:52
// +-----------------------------------------------------
declare(strict_types=1);

namespace app\common\model;

use think\model\concern\SoftDelete;

/**
 * @xk 权限节点模型
 */
class AuthRule extends BaseModel
{
    use SoftDelete;
    protected $name = 'authRule';
    protected $pk = 'auth_rule_id';
    protected $deleteTime = 'delete_time';

    /**
     * @xk 权限节点写入前操作
     *
     * @method onBeforeWrite
     *
     * @param [type] $data [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T10:54:34+080
     */
    public static function onBeforeWrite($data)
    {
        unset($data['act']);
        unset($data['controllers']);
        unset($data['actions']);
        if (!$data->create_time) {
            $data->update_time = $data->create_time = date('Y-m-d H:i:s');
        }
        $data->update_time = date('Y-m-d H:i:s'); // 操作时间 实际发布时间
    }

    /**
     * @xk 获取权限节点树形列表
     *
     * @method getAuthRuleTree
     *
     * @param array $hasNode [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T14:32:18+080
     */
    public static function getAuthRuleTree($hasNode = [])
    {
        $auth_list = self::field('`auth_rule_id`,`auth_name`,`parent_id`,`linkurl`')->select();
        $list = self::doTree($auth_list, 0, $hasNode);
        return $list ? $list : '';
    }

    /**
     * @xk 组装树形列表
     *
     * @method doTree
     *
     * @param [type] $datalist [description]
     * @param int    $pid      [description]
     * @param array  $hasNode  [description]
     *
     * @return [type] [description]
     * @Author xkyinzhe                <zhonghuaxinxing@sina.cn>
     * @Time   2021-04-21T14:32:36+080
     */
    public static function doTree($datalist, $pid = 0, $hasNode = [])
    {
        $tree = [];
        if ($datalist) {
            foreach ($datalist as $key => $value) {
                if ($value['parent_id'] == $pid) {
                    $auth_rule['title'] = $value['auth_name'].'('.$value['linkurl'].')';
                    $auth_rule['auth_name'] = $value['auth_name'];
                    $auth_rule['id'] = $value['auth_rule_id'];
                    if (in_array($value['auth_rule_id'], $hasNode) && 0 != $pid) {
                        $auth_rule['checked'] = true;
                    } else {
                        $auth_rule['checked'] = false;
                    }
                    $auth_rule['children'] = self::doTree($datalist, $value['auth_rule_id'], $hasNode);
                    if (!$auth_rule['children']) {
                        unset($auth_rule['children']);
                    }
                    $tree[] = $auth_rule;
                }
            }
        }

        return $tree;
    }
}
